package com.boot.security.server.controller.system;

import com.boot.security.server.dao.UserDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.dto.UserDto;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.RoleService;
import com.boot.security.server.service.UserService;
import com.boot.security.server.utils.mffl.ProjectUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ApiIgnore("用户管理")
@RestController
@RequestMapping("/mfflUsers")
public class MfflUserController {

    @Autowired
    private MfflUserDao mfflUserDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;


    //默认图片地址
    @Value("${img.path}")
    private String path;

    @PostMapping
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAnyAuthority('sys:mfflUser:add')")
    public ReturnBean save(@RequestBody MfflUser mfflUser) {
        MfflUser mfflUserByPhone = mfflUserDao.getByPhone(mfflUser.getPhone());
        if (mfflUserByPhone != null){
            return new ReturnBean("201","手机号已存在,不可重复!");
        }
        SysUser sysUserByUserName = this.userDao.getUser(mfflUser.getUsername());
        if (sysUserByUserName != null){
            return new ReturnBean("201","用户名已存在,不可重复！");
        }
        SysUser sysUser = new SysUser();
        Date date = new Date();
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(mfflUser,userDto);
        List<Long> dto = new ArrayList<Long>();
        dto.add(roleService.getrole("client").getId());
        userDto.setRoleIds(dto);
        userDto.setPassword("123456");
        userDto.setHeadImgUrl(path);
        userDto.setSex(1);
        sysUser = userService.saveUser(userDto);
        mfflUser.setUserid(sysUser.getId().intValue());
        mfflUser.setRealname("");
        mfflUser.setIdnumber("");
        mfflUser.setOpenid("");
        mfflUser.setUnionid("");
        mfflUser.setAreaCode("");
        mfflUser.setRegisterMethod("手机号");
        mfflUser.setAreaName("");
        mfflUser.setComid(0);
        mfflUser.setThirdlevel("");
        mfflUser.setLevel(0);
        mfflUser.setStartWorkTime(date);
        mfflUser.setWorkCode("");
        mfflUser.setLawfirm("");
        mfflUser.setField("");
        mfflUser.setServerNumber(0);
        mfflUser.setRating("100");
        mfflUser.setWorkBootImage("");
        mfflUser.setEducation("");
        mfflUser.setContent("");
        mfflUser.setIntegral(0);
        mfflUser.setGradeIntegral("");
        mfflUser.setSearchLike("");
        mfflUser.setIdnumberImage("");
        mfflUser.setCreateTime(date);
        mfflUser.setUpdateTime(date);
        mfflUser.setIdentity(0);
        mfflUser.setDeleteTime(date);
        mfflUser.setDeleteState(1);
        mfflUser.setLawyerInspect(0);
        mfflUser.setMoney(ProjectUtil.addition(0.0,0.0));
        mfflUser.setSecondusername(ProjectUtil.getRandomString(12));//随机用户名
        mfflUserDao.save(mfflUser);

        return new ReturnBean();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    @PreAuthorize("hasAnyAuthority('sys:mfflUser:query')")
    public MfflUser get(@PathVariable Long id) {

        return mfflUserDao.getUserInfoAll(id.intValue());
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:mfflUser:add')")
    public ReturnBean update(@RequestBody MfflUser mfflUser) {


        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(mfflUser,sysUser);
        sysUser.setId(mfflUser.getUserid().longValue());
        int status = this.userDao.update(sysUser);
        if (status == 0){
            return new ReturnBean("201","修改失败");
        }
        return new ReturnBean();
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:mfflUser:query')")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflUserDao.userCount(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflUser> list(PageTableRequest request) {
                return mfflUserDao.userList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mfflUserDao.delete(id);
    }
}
